library(tidyverse)  # loads packages in the tidyverse (dplyr, ggplot2, etc,)
library(haven)  # let's us read files from outside packages (Stata, SPSS, SAS)
library(wesanderson) # loads the Wes Anderson color palette 
library(grid) # for plotting
library(gridExtra) # need for the final grid.arrange function at the end to combine the plots

# figure 1: full data

# load in the data
df_house <- haven::read_dta('House 1.dta')
df_senate <- haven::read_dta('Senate 1.dta')
df <- dplyr::bind_rows(df_house,df_senate)

# remove the variables in this data that we don't need
rm(df_house,df_senate)
df <- dplyr::select(df, party, patriotism, selfreliance, socialorder, freedom, morality, tolerance, equality)

#make the data a tibble (improved data.frame) - works well with the ggplot2 package
df <- as_tibble(df)
new_df <- df %>%
  tidyr::gather(equality, tolerance, morality,
                freedom, socialorder, selfreliance, patriotism, key = "values", value = "mean_val")

#rename variables for plot
new_df <- new_df %>%
  mutate(
    values = str_replace(values, 'selfreliance', 'Self-Reliance'),
    values = str_replace(values, 'socialorder', 'Social Order'),
    values = str_replace(values, 'morality', 'Moral Traditionalism'),
    values = str_replace(values, 'tolerance', 'Moral Tolerance'),
    values = str_to_title(values)
  )

# create figures
group_df <- new_df %>%
  group_by(values, party) %>%
  summarize(val_mean = mean(mean_val, na.rm=T),
            val_se = sd(mean_val, na.rm = T)/sqrt(n())) 
group_df <- dplyr::filter(group_df, !is.na(party))
group_df$ll <- group_df$val_mean - (1.96 * group_df$val_se)
group_df$ul <- group_df$val_mean + (1.96 * group_df$val_se)
group_df$party <- factor(group_df$party)
levels(group_df$party) <- c('Democrat', 'Republican')
group_df$values <- factor(group_df$values)

ggplot(data = group_df, aes(x = values, y = val_mean, fill = party)) + 
  geom_bar(stat = "identity", position = "dodge") + labs(x = " ", y = "Mean Value Endorsements") +
  ggtitle("") +
  scale_fill_manual(values = c("#103f69", "#6b1318")) +
  theme_classic() +
  theme(axis.ticks = element_blank(), legend.position = "bottom",
        legend.justification = 'center', 
        plot.title = element_text(hjust = 0.5, size = 20, face = 'bold'),
        plot.margin = unit(c(1,1,1,1), "cm"),
        legend.title = element_blank(),
        legend.text = element_text(face = 'bold', size = 9),
        axis.title = element_text(face = 'bold', size = 10,
                                  margin = margin(t = 20)),
        axis.text.y = element_text(size = 10)) +
  scale_x_discrete(limits = c("Social Order","Moral Tolerance","Freedom","Moral Traditionalism","Equality","Patriotism","Self-Reliance")) +
  guides(color = guide_legend(override.aes = list(linetype = 0))) +
  coord_flip()

# figure 2: stripped down variables

# load in the data
df_house <- haven::read_dta('House 2.dta')
df_senate <- haven::read_dta('Senate 2.dta')
df <- dplyr::bind_rows(df_house,df_senate)

# remove the variables in this data that we don't need
rm(df_house,df_senate)
df <- dplyr::select(df, party, patriotism, selfreliance, socialorder, freedom, morality, tolerance, equality)

#make the data a tibble (improved data.frame) - works well with the ggplot2 package
df <- as_tibble(df)
new_df <- df %>%
  tidyr::gather(equality, tolerance, morality,freedom, socialorder, selfreliance, patriotism, key = "values", value = "mean_val")

#rename variables for plot
new_df <- new_df %>%
  mutate(
    values = str_replace(values, 'selfreliance', 'Self-Reliance'),
    values = str_replace(values, 'socialorder', 'Social Order'),
    values = str_replace(values, 'morality', 'Moral Traditionalism'),
    values = str_replace(values, 'tolerance', 'Moral Tolerance'),
    values = str_to_title(values)
  )

# create figures
group_df <- new_df %>%
  group_by(values, party) %>%
  summarize(val_mean = mean(mean_val, na.rm=T),
            val_se = sd(mean_val, na.rm = T)/sqrt(n())) 
group_df <- dplyr::filter(group_df, !is.na(party))
group_df$ll <- group_df$val_mean - (1.96 * group_df$val_se)
group_df$ul <- group_df$val_mean + (1.96 * group_df$val_se)
group_df$party <- factor(group_df$party)
levels(group_df$party) <- c('Democrat', 'Republican')
group_df$values <- factor(group_df$values)

ggplot(data = group_df, aes(x = values, y = val_mean, fill = party)) + 
  geom_bar(stat = "identity", position = "dodge") + labs(x = " ", y = "Mean Value Endorsements") +
  ggtitle("") +
  scale_fill_manual(values = c("#103f69", "#6b1318")) +
  theme_classic() +
  theme(axis.ticks = element_blank(), legend.position = "bottom",
        legend.justification = 'center', 
        plot.title = element_text(hjust = 0.5, size = 20, face = 'bold'),
        plot.margin = unit(c(1,1,1,1), "cm"),
        legend.title = element_blank(),
        legend.text = element_text(face = 'bold', size = 9),
        axis.title = element_text(face = 'bold', size = 10,
                                  margin = margin(t = 20)),
        axis.text.y = element_text(size = 10)) +
  scale_x_discrete(limits = c("Social Order","Moral Tolerance","Freedom","Moral Traditionalism","Equality","Patriotism","Self-Reliance")) +
  guides(color = guide_legend(override.aes = list(linetype = 0))) +
  coord_flip()
